<?php
/**
 * Class that operate on table 'notes'. Database Sqlite.
 *
 * @author: Dragan Milchevski
 * @date: 2012-06-23 13:23
 */
class NotesSqliteDAO implements NotesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return NotesSqlite 
	 */
	public function load($id){
		$sql = 'SELECT * FROM notes WHERE notesID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM notes';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM notes ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param note primary key
 	 */
	public function delete($notesID){
		$sql = 'DELETE FROM notes WHERE notesID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($notesID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param NotesSqlite note
 	 */
	public function insert($note){
		$sql = 'INSERT INTO notes (userID, fileID, notes) VALUES (?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($note->getUser()->getId());
		$sqlQuery->setNumber($note->getDocument()->getId());
		$sqlQuery->set($note->getNote());

		$id = $this->executeInsert($sqlQuery);	
		$note->setId($id);
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param NotesSqlite note
 	 */
	public function update($note){
		$sql = 'UPDATE notes SET userID = ?, fileID = ?, notes = ? WHERE notesID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($note->getId());
		$sqlQuery->setNumber($note->getId());
		$sqlQuery->set($note->getNote());

		$sqlQuery->setNumber($note->getId());
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM notes';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByUserID($value){
		$sql = 'SELECT * FROM notes WHERE userID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFileID($value){
		$sql = 'SELECT * FROM notes WHERE fileID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByNotes($value){
		$sql = 'SELECT * FROM notes WHERE notes = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByUserID($value){
		$sql = 'DELETE FROM notes WHERE integer = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFileID($value){
		$sql = 'DELETE FROM notes WHERE integer = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNotes($value){
		$sql = 'DELETE FROM notes WHERE text = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return NotesSqlite 
	 */
	protected function readRow($row){
		$note = new Note();
		
		$note->setId($row['notesID']);
		$note->setUser($row['userID']);
		$note->setDocument($row['fileID']);
		$note->setNote($row['notes']);

		return $note;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return NotesSqlite 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>